package com.hhxy.handler;

import com.alibaba.fastjson.JSON;
import com.hhxy.utils.ResponseResult;
import com.hhxy.utils.WebUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ghp
 * @date 2023/3/9
 * @title
 * @description
 */
@Component
public class AccessDeniedImpl implements AccessDeniedHandler {
    /**
     * 处理权限不足时的异常
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        // 用户登录，但是没有这个权限
        ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED.value(), "您的权限不足");
        String jsonString = JSON.toJSONString(result);
        WebUtil.renderString(response, jsonString);
    }
}
